package com.cryptoneedle.garden.vo;


import com.cryptoneedle.garden.entity.source.metadata.SourceCatalog;
import com.cryptoneedle.garden.entity.source.metadata.SourceServer;
import com.cryptoneedle.garden.enums.SourceCollectFrequency;
import com.cryptoneedle.garden.enums.SourceConnectType;
import com.cryptoneedle.garden.enums.SourceDatabaseType;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.validation.constraints.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.apache.commons.lang3.StringUtils;

/**
 * <p>description:  </p>
 *
 * @author CryptoNeedle
 * @date 2025-06-23
 */
@Data
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
public class CatalogSaveVo {

    @NotBlank(message = "源数据源-目录名称不能为空")
    @Size(max = 128, message = "源数据源-目录名称长度不能超过128个字符")
    @Schema(name = "源数据源-目录名称", description = "源数据源的目录名称，最大长度128字符", example = "巡检管理系统")
    private String catalogName;

    @NotBlank(message = "服务器主机不能为空")
    @Pattern(regexp = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])|([a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]\\.)+[a-zA-Z]{2,}$", message = "服务器主机必须是有效的IP地址或域名")
    @Schema(name = "服务器-主机", description = "服务器主机名或IP地址", example = "192.168.1.100 或 db.example.com")
    private String host;
    @NotNull(message = "服务器端口不能为空")
    @Min(value = 1, message = "端口号不能小于1")
    @Max(value = 65535, message = "端口号不能大于65535")
    @Schema(name = "服务器-端口", description = "服务器端口号", example = "3306")
    private Integer port;

    @NotNull(message = "数据库类型不能为空")
    @Schema(name = "元数据-数据库类型", description = "数据库类型枚举", example = "MYSQL")
    private SourceDatabaseType databaseType;
    @NotBlank(message = "JDBC路径不能为空")
    @Size(max = 128, message = "JDBC路径长度不能超过128个字符")
    @Schema(name = "JDBC-路径", description = "JDBC连接路径", example = "jdbc:mysql://localhost:3306/dbname中的dbname")
    private String route;
    @NotBlank(message = "数据库用户名不能为空")
    @Size(max = 128, message = "数据库用户名长度不能超过128个字符")
    @Schema(name = "元数据-用户名称", description = "数据库用户名", example = "mysql")
    private String userName;
    @NotBlank(message = "数据库密码不能为空")
    @Size(min = 6, max = 128, message = "数据库密码长度应在6-128个字符之间")
    @Schema(name = "元数据-密码", description = "数据库密码", example = "securePassword123")
    private String password;
    @NotNull(message = "JDBC连接类型不能为空")
    @Enumerated(EnumType.STRING)
    @Schema(name = "JDBC-连接类型", description = "JDBC连接类型枚举", example = "SERVICE_NAME")
    private SourceConnectType connectType;

    @NotBlank(message = "Doris目录名称不能为空")
    @Size(max = 50, message = "Doris目录名称长度不能超过50个字符")
    @Pattern(regexp = "^[a-z]+$", message = "系统编码只能包含小写字母")
    @Schema(name = "源数据源-Doris目录名称", description = "Doris目录名称", example = "patrol")
    private String dorisCatalogName;
    @Size(max = 50, message = "转换系统编码长度不能超过50个字符")
    @Pattern(regexp = "^$|^[a-z]+$", message = "系统编码只能包含小写字母")
    @Schema(name = "源数据源-系统编码", description = "目标系统的唯一编码", example = "patrol")
    private String defaultSystemCode;

    public SourceServer createServer() {
        return new SourceServer().setHost(this.host).setPort(this.port).setServerConnected(false).setServerConnectedDt(null);
    }

    public SourceCatalog createCatalog() {
        return new SourceCatalog()
                .setCatalogName(this.catalogName)
                .setHost(this.host)
                .setPort(this.port)
                .setDatabaseType(this.databaseType)
                .setRoute(this.route)
                .setUserName(this.userName)
                .setPassword(this.password)
                .setConnectType(this.connectType)
                .setDriverClass(this.databaseType.getDatabase().getJdbcDriverClass())
                .setUrl(this.databaseType.getDatabase().getJdbcUrl(this.connectType, this.host, this.port, this.route))
                .setDatabaseVersion(null)
                .setDorisCatalogName(StringUtils.isNotBlank(this.dorisCatalogName) ? this.dorisCatalogName : null)
                .setDefaultSystemCode(StringUtils.isNotBlank(this.defaultSystemCode) ? this.defaultSystemCode : null)
                .setDefaultCollectFrequency(SourceCollectFrequency.HOUR)
                .setEnabled(true)
                .setServerConnected(false)
                .setServerConnectedDt(null)
                .setJdbcConnected(false)
                .setJdbcConnectedDt(null)
                .setDorisConnected(false)
                .setDorisConnectedDt(null);
    }
}